package com.apes.crm.business.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname 保险公司
 * @Description TODO
 * @Date 2019/12/24 10:53
 * @Created by wsj
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsuranceCompany.CONST_TABLE_NAME)
public class InsuranceCompany extends Model {
    public static final String CONST_TABLE_NAME = "crm_insurance_company";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "保险公司",padding = 4)
    @Comment("保险公司编码")
    private String id;

    @Column(name = "name", length = 120)
    @Comment("保险公司名称")
    private String name;  //显示编码

    @Column(name = "abbreviation", length = 32)
    @Comment("简称")
    private String abbreviation;

    @Column(name = "APP_Code", length = 32)
    @Comment("APP对应编码")
    private String APPCode;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "client_id", referencedColumnName = "id")
    @Comment("客户")
    private PartyRole client;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商")
    private PartyRole supplier;

    @Column(name = "cooperation",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("合作标记")
    private boolean cooperation = false;

    @Column(name = "sources_type", length = 32 , nullable = false , columnDefinition = "varchar2(32) default '平台数据'")  // 历史数据来源单据都为平台数据
    @Comment("数据来源")
    private String sourcesType; // 门店数据;平台数据;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false , columnDefinition = "varchar2(32) default '1001'") // 历史数据所属公司都为1001
    @Comment("所属公司")
    private PartyRole company;

    @OneToMany(mappedBy = "insuranceCompany", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<InsuranceCompanyDetail> insuranceCompanyDetails = new ArrayList<>();

    public List<InsuranceCompanyDetail> getInsuranceCompanyDetails() {
        if (insuranceCompanyDetails == null) return new ArrayList<>();
        for (InsuranceCompanyDetail item : insuranceCompanyDetails) {
            item.setInsuranceCompany(this);
        }
        return insuranceCompanyDetails;
    }

}
